package com.shilun.demo;



import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

/**
 * @author sl shilun217@qq.com
 * @Deacription
 * @date 2021/11/20/21:47
 **/
public class GraphDatabaseHelloWorld {

    private static final File DB_PATH = new File("D:\\Neo4jDb");
    private static GraphDatabaseService graphDb;

    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                graphDb.shutdown();
            }
        });
    }

    private static enum RelTypes implements RelationshipType {
        RELEASED;
    }

    @SuppressWarnings("unused")
    private static void addData() {
        Node node1;
        Node node2;
        Label label1;
        Label label2;
        Relationship relationship;

        try (Transaction tx = graphDb.beginTx()) {
            // 创建标签
            label1 = Label.label("Musician");
            label2 = Label.label("Album");
            // 创建节点
            node1 = graphDb.createNode(label1);
            node1.setProperty("name", "Jay Chou");
            node2 = graphDb.createNode(label2);
            node2.setProperty("name", "Fantasy");
            // 创建关系及属性
            relationship = node1.createRelationshipTo(node2, RelTypes.RELEASED);
            relationship.setProperty("date", "2001-09-14");
            relationship.getType();
            // 结果输出
            System.out.println("created node name is " + node1.getProperty("name"));
            System.out.println(relationship.getProperty("date"));
            System.out.println( relationship.getType());
            System.out.println("created node name is " + node2.getProperty("name"));
            // 提交事务
            tx.success();
        }
        //graphDb.shutdown();
    }

    @SuppressWarnings("unused")
    private static void queryAndUpdate() {
        try (Transaction tx = graphDb.beginTx()) {
            // 查询节点
            Label label = Label.label("Musician");
            Node node = graphDb.findNode(label, "name", "Jay Chou");
            System.out.println("query node name is " + node.getProperty("name"));
            // 更新节点
            node.setProperty("birthday", "1979-01-18");
            System.out
                    .println(node.getProperty("name") + "'s birthday is " + node.getProperty("birthday", new String()));
            // 提交事务
            tx.success();
        }
        //graphDb.shutdown();
    }
    
    /*
    * @Description  
    * @Author shilun
    * @CreateTime 2021/11/20 22:15  * @param 
    * @Return void
    * @Throws 
    */
    private static void query(){
        String query = "match(n) return id(n) as id,labels(n) as lb,n.name as name order by id desc";
        Map<String,Object> parameters = new HashMap<>();
        try(Result result = graphDb.execute(query, parameters)){
            while(result.hasNext()){
                Map<String, Object> row = result.next();
                result.columns().forEach((key)->{
                    System.out.printf("query------------%s = %s%n", key, row.get(key));
                });

            }
        }
    }

    @SuppressWarnings("unused")
    private static void delete() {
        try (Transaction tx = graphDb.beginTx()) {
            // 获得节点
            Label label = Label.label("Album");
            Node node = graphDb.findNode(label, "name", "Fantasy");
            // 获得关系
            Relationship relationship = node.getSingleRelationship(RelTypes.RELEASED, Direction.INCOMING);
            // 删除关系和节点
            relationship.delete();
            relationship.getStartNode().delete();
            node.delete();
            System.out.println("delete删除");
            tx.success();
        }

    }

    public static void main(String[] args) {
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
        registerShutdownHook(graphDb);
        addData();
        query();
        queryAndUpdate();
        query();
        delete();
        query();
    }

}